package com.lelouch.controller;

import com.lelouch.common.JsonData;
import com.lelouch.dto.DeptLevelDto;
import com.lelouch.param.DeptParam;
import com.lelouch.service.SysDeptService;
import com.lelouch.service.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 部门控制器
 *
 * @author lcy
 * @create 2018-06-12 15:05
 */
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysTreeService sysTreeService;


    @RequestMapping("/dept.page")
    @ResponseBody
    public ModelAndView index(){
        return new ModelAndView("dept");
    }


    /**
     * 新增部门信息
     * @param deptParam
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(DeptParam deptParam){
        sysDeptService.save(deptParam);
        return JsonData.success("新添部门成功");
    }

    /**
     * 列出部门树
     * @return
     */
    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DeptLevelDto> dtoList = sysTreeService.deptTree();
        return JsonData.success(dtoList);
    }

    /**
     * 更新部门信息
     * @param deptParam
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(DeptParam deptParam){
        sysDeptService.update(deptParam);
        return JsonData.success("更新部门成功");
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("deptId") Integer deptId){
        sysDeptService.delele(deptId);
        return JsonData.success("删除部门成功");
    }

}
